package com.lemon.smartsafety.ui.adapter.address;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.entity.common.IndexModel;
import com.lemon.smartsafety.entity.event.address.InviteHandleEvent;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/29.
 */
public class AddressInviteAdapter extends LemonRcvAdapter<IndexModel> {

    private LoggerHelper loggerHelper = new LoggerHelper(AddressInviteAdapter.class);

    private RecyclerTClickListener<IndexModel> mListener;

    public AddressInviteAdapter(@Nullable List<IndexModel> data) {
        super(data);
    }

    public AddressInviteAdapter(List<IndexModel> data, RecyclerTClickListener<IndexModel> listener) {
        super(data);
        this.mListener = listener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new AddressMainItem();
    }

    public class AddressMainItem implements AdapterItem<IndexModel> {

        LinearLayout   ripple;
        TextView     tvTip;
        TextView     tvTitle;
        TextView     tvSubTitle;
        TextView     tvStatusTip;
        LinearLayout layoutHandler;
        ImageView    ivSure;
        ImageView    ivCancel;


        private IndexModel mData;
        private int        mPosition;


        @Override
        public int getLayoutResId() {
            return R.layout.item_address_book_other;
        }

        @Override
        public void bindViews(View root) {
            ripple = (LinearLayout) root.findViewById(R.id.ripple);
            tvTip = (TextView) root.findViewById(R.id.tv_tip);
            tvTitle = (TextView) root.findViewById(R.id.tv_title);
            tvSubTitle = (TextView) root.findViewById(R.id.tv_sub_title);
            tvStatusTip = (TextView) root.findViewById(R.id.tv_status_tip);
            ivSure = (ImageView) root.findViewById(R.id.iv_sure);
            ivCancel = (ImageView) root.findViewById(R.id.iv_cancel);
            layoutHandler = (LinearLayout) root.findViewById(R.id.ll_handle);

            ripple.setEnabled(false);

        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });
      /*      ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });*/

            ivSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    InviteHandleEvent event = new InviteHandleEvent();
                    event.setTag(InviteHandleEvent.class.getName());
                    event.setData(mData);
                    event.setType(1);
                    AppService.getBus().post(event);
                }
            });
            ivCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    InviteHandleEvent event = new InviteHandleEvent();
                    event.setTag(InviteHandleEvent.class.getName());
                    event.setData(mData);
                    event.setType(0);
                    AppService.getBus().post(event);
                }
            });
        }

        @Override
        public void handleData(IndexModel data, int position) {
            mData = data;
            mPosition = position;
            tvTip.setText(data.getIndexCharI());
            tvTitle.setText(data.getTitleI());
            tvSubTitle.setText(StringUtils.nullValue(data.getSubTitleI()));

            if (data.getStatusI() == 0) {
                tvStatusTip.setText(data.getStatusTip());
                layoutHandler.setVisibility(View.VISIBLE);
                tvStatusTip.setVisibility(View.GONE);
            } else if (data.getStatusI() == 1) {//agree
                layoutHandler.setVisibility(View.GONE);
                tvStatusTip.setVisibility(View.VISIBLE);
                tvStatusTip.setText(StringUtils.getStatusString(data.getStatusTip(), StringUtils.ColorStatus.STATUS_GREEN));
            } else if (data.getStatusI() == 2) {
                layoutHandler.setVisibility(View.GONE);
                tvStatusTip.setVisibility(View.VISIBLE);
                tvStatusTip.setText(StringUtils.getStatusString(data.getStatusTip(), StringUtils.ColorStatus.STATUS_GRAY));
            }


            int section = getSectionForPosition(position);
            if (position == getPositionForSection(section)) {
                tvTip.setVisibility(View.VISIBLE);
            } else {
                tvTip.setVisibility(View.GONE);
            }
        }

    }


    public int getSectionForPosition(int position) {
        return mDataList.get(position).getIndexCharI().charAt(0);
    }

    public int getPositionForSection(int section) {
        for (int i = 0; i < getItemCount(); i++) {
            String sortStr   = mDataList.get(i).getIndexCharI();
            char   firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }
}
